package com.thon.controller.rest;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @File: InstitutionRestController.java
* @author zemo 
* @mail chenzeming@lanbaoo.com
* @date: Jan 23, 2014 9:40:47 AM
* @Description: TODO
 */
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.thon.dao.util.Page;
import com.thon.entity.user.Charitarian;
import com.thon.entity.user.Institution;
import com.thon.service.user.CharitarianService;
import com.thon.service.user.InstitutionService;
@Controller
@RequestMapping(value = "/api")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class RolesRestController {

	@Autowired
	private InstitutionService institutionService;
	@Autowired
	private CharitarianService charitarianService;
	
	@RequestMapping(value = "/institution/add",method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> addInstitution(@Valid @RequestBody Institution institution){
		
		Byte status = 1;
		institution.setStatus(status);
		institutionService.addInstitution(institution);
		return new ResponseEntity(HttpStatus.CREATED);
	}
	
	@RequestMapping(value="/institution/list",method = RequestMethod.GET)
	public ResponseEntity<HttpEntity> listInstitutionByStatus(
			@RequestParam(value = "status",required = true)Byte status,
			@RequestParam(value = "p",defaultValue = "1")int pageNo,
			@RequestParam(value = "s",defaultValue = "10")int pageSize){
		Page<Institution> page = new Page<Institution>();
		page = institutionService.findInstitutionByStatus(status, pageSize, pageNo);
		return new ResponseEntity(page,HttpStatus.OK);
	}
	
	@RequestMapping(value = "/charitarian/add",method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> addCharitarian(@Valid @RequestBody Charitarian charitarian){
		
		if(charitarianService.findCharitarianByUserId(charitarian.getUser().getId())==null){
			Byte status = 1;
			charitarian.setStatus(status);
			charitarianService.addCharitarian(charitarian);
			return new ResponseEntity(HttpStatus.OK);
		}else{
			return new ResponseEntity("false",HttpStatus.OK);
		}
	}
	
	@RequestMapping(value="/charitarian/list",method = RequestMethod.GET)
	public ResponseEntity<HttpEntity> listCharitarianByStatus(
			@RequestParam(value = "status",required = true)Byte status,
			@RequestParam(value = "p",defaultValue = "1")int pageNo,
			@RequestParam(value = "s",defaultValue = "10")int pageSize){
		Page<Charitarian> page = new Page<Charitarian>();
		page = charitarianService.findCharitarian(status, pageSize, pageNo);
		return new ResponseEntity(page,HttpStatus.OK);
	}
	
	@RequestMapping(value="/user/check",method = RequestMethod.GET)
	public ResponseEntity<HttpEntity> checkUser(
			@RequestParam(value = "uid")int uid){
		Institution institution = institutionService.getInstitutionByUser(uid);
		if(institution!=null){
			return new ResponseEntity(institution,HttpStatus.OK);		
		}else{
			return new ResponseEntity(0,HttpStatus.OK);
		}
	}
}
